package com.share.ocr;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLDecoder;
import java.util.HashMap;

/**
 * author:caifan
 * date:2019/10/6
 */
public class OCRDemo_01 {
    public static final String APP_ID = "17443928";
    public static final String API_KEY = "wypdLI6RXN7Se41FrmFj3yo3";
    public static final String SECRET_KEY = "Xr2PQ2iogFkQDXY8wmcfU1cgGpu1IKzC";

    public static void main(String[] args) throws Exception {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "./baidu/log4j.properties");

        // 调用接口
        String path = "D:/data/test.png";
//        client.basicGeneral()
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        JSONArray wordsResults = res.getJSONArray("words_result");
        StringBuffer buffer = new StringBuffer();
        for (Object obj : wordsResults) {
            JSONObject json  = (JSONObject)obj;
            String word = json.getString("words");
            word = URLDecoder.decode(word, "UTF-8");
            buffer.append(word);
        }
        System.out.println(buffer);
    }
}
